package com.bw.utils.esypoi;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.BeanUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * @author Monte
 * @author 644527030@qq.com
 * @author 知识改变命运，代码改变世界！
 * @date 2024年07月31日    10:14
 * @PackageName:com.bw.utils.esypoi
 * @ClassName: ExportsUtil
 * @Description HomeController
 */

public class ExportsUtil {

    public static <T> void exports(HttpServletResponse httpServletResponse, Class<?> classEntity, List<T> data, String sheetName, String title, String fileName){

        try {
            // 设置要导出的基本的数据
            ExportParams exportParams =
                    new ExportParams(title, sheetName, ExcelType.XSSF);
//            xls ExcelType.HSSF

            // 返回一个工作簿
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, classEntity, data);

            // 设置浏览器文件下载
            httpServletResponse.setHeader("Content-Disposition",
                    "attachment;filename="+ fileName +".xlsx");

            // 写入输出流
            workbook.write(httpServletResponse.getOutputStream());

            // 关闭输出流
            httpServletResponse.getOutputStream().close();

            // 关闭工作簿
            workbook.close();
        }catch (IOException e){
            e.printStackTrace();
        }

    }


    public static <T> void exportsStyle(HttpServletResponse httpServletResponse, Class<?> classEntity, List<T> data, String sheetName, String title, String fileName, List<Integer[]> stylePointList, HashMap<String, Object> styleMap){

        try {
            // 设置要导出的基本的数据
            ExportParams exportParams =
                    new ExportParams(title, sheetName, ExcelType.XSSF);

            // 返回一个工作簿
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, classEntity, data);

            // 预定义样式
            CellStyle cellStyle = workbook.createCellStyle();
            // 默认水平垂直居中
            cellStyle.setAlignment(HorizontalAlignment.CENTER);
            cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

            // 字体颜色
            if (styleMap.get("color") != null){
                Font font = workbook.createFont();
                font.setColor((short) styleMap.get("color"));
                cellStyle.setFont(font);
            }
            // 背景色
            if (styleMap.get("background") != null){
                cellStyle.setFillForegroundColor((short) styleMap.get("background"));
                cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            }


            // 设置样式开始
            // 1、获取单元格
            Sheet sheet = workbook.getSheet(sheetName);
            // 2、循环获取行单元格
            for (Integer[] point : stylePointList){
                // 所属行
                Integer rowNum = point[0];
                // 单元格
                Integer cellNum = point[1];
                // 设置单元格样式
                sheet.getRow(rowNum).getCell(cellNum).setCellStyle(cellStyle);
            }

            // 设置样式结束


            // 设置浏览器文件下载
            httpServletResponse.setHeader("Content-Disposition",
                    "attachment;filename="+ fileName +".xlsx");

            // 写入输出流
            workbook.write(httpServletResponse.getOutputStream());

            // 关闭输出流
            httpServletResponse.getOutputStream().close();

            // 关闭工作簿
            workbook.close();
        }catch (IOException e){
            e.printStackTrace();
        }

    }

    public static <T> void exportsStyle(HttpServletResponse httpServletResponse, Class<?> classEntity, List<T> data, String sheetName, String title, String fileName, List<Integer[]> stylePointList, List<HashMap<String, Object>> styleMap){

        try {
            // 设置要导出的基本的数据
            ExportParams exportParams =
                    new ExportParams(title, sheetName, ExcelType.XSSF);

            // 返回一个工作簿
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, classEntity, data);

            // 设置样式开始
            // 1、获取单元格
            Sheet sheet = workbook.getSheet(sheetName);
            // 2、循环获取行单元格
            int i=0;
            for (Integer[] point : stylePointList){

                // 预定义样式
                CellStyle cellStyle = workbook.createCellStyle();
                // 默认水平垂直居中
                cellStyle.setAlignment(HorizontalAlignment.CENTER);
                cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);


                // 字体颜色
                if (styleMap.get(i).get("color") != null){
                    Font font = workbook.createFont();
                    font.setColor((short) styleMap.get(i).get("color"));
                    cellStyle.setFont(font);
                }
                // 背景色
                if (styleMap.get(i).get("background") != null){
                    cellStyle.setFillForegroundColor((short) styleMap.get(i).get("background"));
                    cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                }

                // 所属行
                Integer rowNum = point[0];
                // 单元格
                Integer cellNum = point[1];
                // 设置单元格样式
                sheet.getRow(rowNum).getCell(cellNum).setCellStyle(cellStyle);
                i++;
            }

            // 设置样式结束


            // 设置浏览器文件下载
            httpServletResponse.setHeader("Content-Disposition",
                    "attachment;filename="+ fileName +".xlsx");

            // 写入输出流
            workbook.write(httpServletResponse.getOutputStream());

            // 关闭输出流
            httpServletResponse.getOutputStream().close();

            // 关闭工作簿
            workbook.close();
        }catch (IOException e){
            e.printStackTrace();
        }

    }
}
